package com.teacher.service.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.teacher.pojo.entity.AbsentClass;
import com.teacher.pojo.entity.Volunteer;
import com.teacher.pojo.excel.AbsentClassExcel;
import com.teacher.pojo.excel.VolunteerExcel;
import com.teacher.service.IAbsentClassService;
import com.teacher.service.IPerStudentService;
import com.teacher.service.IVolunteerService;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zfm
 * @Date: 2021/7/30 16:17
 */
public class ExcelVolunteerListener extends AnalysisEventListener<VolunteerExcel> {
    private Long termId;
    private IVolunteerService iVolunteerService;

    private List<Volunteer> volunteerList = new ArrayList<>();
    private IPerStudentService iPerStudentService;

    public ExcelVolunteerListener(Long termId, IVolunteerService iVolunteerService, IPerStudentService iPerStudentService) {
        this.termId = termId;
        this.iVolunteerService = iVolunteerService;
        this.iPerStudentService = iPerStudentService;
    }

    @Override
    public void invoke(VolunteerExcel volunteerExcel, AnalysisContext analysisContext) {
        if (iPerStudentService.isExit(volunteerExcel.getStudentKey())) {
            Volunteer volunteer = new Volunteer();
            BeanUtils.copyProperties(volunteerExcel, volunteer);
            volunteer.setTermId(termId);
            volunteerList.add(volunteer);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        saveData();
    }

    private void saveData() {
        iVolunteerService.saveBatch(volunteerList);
    }
}
